package com.cpimex.eprice.action.plugin;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionBuilder {
	
	
	/**
	 * Conexion Eprice
	 * 
	 * @param context
	 * @return
	 * @throws SQLException
	 */
	public static Connection epriceConnection( InitialContext context ) throws Exception{
		Connection conn = null;
		
		DataSource dataSrc = null;
		try {
			dataSrc = (DataSource) context.lookup("java:comp/env/Eprice");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		try {
			dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		try{
			conn  = dataSrc.getConnection();
		}catch(Exception e){
			//System.out.println("Conexion con Driver");
			Class.forName("com.inet.tds.TdsDriver");
			conn = java.sql.DriverManager.getConnection("jdbc:inetdae:sqadc01:1433?database=eprice", "eprice", "eprice" );
		}
		return conn;
	}
	
	
	/**
	 * Conexion modulo Modulo de Seguridad 
	 * 
	 * @param context
	 * @return
	 * @throws SQLException
	 */
	public static Connection msConnection(InitialContext context ) throws SQLException{
		Connection conn = null;
		
		DataSource dataSrc = null;
		try {
			dataSrc = (DataSource) context.lookup("java:comp/env/ModSeg");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		try {
			dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/ModSeg");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		conn  = dataSrc.getConnection();
		return conn;
	}
	
	
	/**
	 * Conexion modulo BaaN 
	 * 
	 * @param context
	 * @return
	 * @throws SQLException
	 */
	public static Connection baanConnection(InitialContext context ) throws SQLException{
		Connection conn = null;
		
		DataSource dataSrc = null;
		try {
			dataSrc = (DataSource) context.lookup("java:comp/env/BaaN");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		try {
			dataSrc = (DataSource) context.lookup("java:BaaN");
		} catch (NamingException e) {
			//e.printStackTrace();
		}
		conn  = dataSrc.getConnection();
		return conn;
	}
	
	
	
	
	
}	
